home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / WordPress 1.5.1.dmg / wordpress / wp-admin / upload.php < prev    next >
Encoding:
PHP Script  |  2005-02-16  |  8.5 KB  |  230 lines

  1. <?php
  2. require_once('admin.php');
  3.  
  4. $title = 'Upload Image or File';
  5.  
  6. require_once('admin-header.php');
  7.  
  8. if ($user_level == 0) //Checks to see if user has logged in
  9.     die (__("Cheatin' uh ?"));
  10.  
  11. if (!get_settings('use_fileupload')) //Checks if file upload is enabled in the config
  12.     die (__("The admin disabled this function"));
  13.  
  14. if ( !get_settings('fileupload_minlevel') )
  15.     die (__("You are not allowed to upload files"));
  16.  
  17. $allowed_types = explode(' ', trim(strtolower(get_settings('fileupload_allowedtypes'))));
  18.  
  19. if ($_POST['submit']) {
  20.     $action = 'upload';
  21. } else {
  22.     $action = '';
  23. }
  24.  
  25. if (!is_writable(get_settings('fileupload_realpath')))
  26.     $action = 'not-writable';
  27. ?>
  28.  
  29. <div class="wrap">
  30.  
  31. <?php
  32. switch ($action) {
  33. case 'not-writable':
  34. ?>
  35. <p><?php printf(__("It doesn't look like you can use the file upload feature at this time because the directory you have specified (<code>%s</code>) doesn't appear to be writable by WordPress. Check the permissions on the directory and for typos."), get_settings('fileupload_realpath')) ?></p>
  36.  
  37. <?php
  38. break;
  39. case '':
  40.     foreach ($allowed_types as $type) {
  41.         $type_tags[] = "<code>$type</code>";
  42.     }
  43.     $i = implode(', ', $type_tags);
  44. ?>
  45. <p><?php printf(__('You can upload files with the extension %1$s as long as they are no larger than %2$s <abbr title="Kilobytes">KB</abbr>. If you’re an admin you can configure these values under <a href="%3$s">options</a>.'), $i, get_settings('fileupload_maxk'), 'options-misc.php') ?></p>
  46.     <form action="upload.php" method="post" enctype="multipart/form-data">
  47.     <p>
  48.       <label for="img1"><?php _e('File:') ?></label>
  49.       <br />
  50.     <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_settings('fileupload_maxk') * 1024 ?>" />
  51.     <input type="file" name="img1" id="img1" size="35" class="uploadform" /></p>
  52.     <p>
  53.     <label for="imgdesc"><?php _e('Description:') ?></label><br />
  54.     <input type="text" name="imgdesc" id="imgdesc" size="30" class="uploadform" />
  55.     </p>
  56.     
  57.     <p><?php _e('Create a thumbnail?') ?></p>
  58.     <p>
  59.     <label for="thumbsize_no">
  60.     <input type="radio" name="thumbsize" value="none" checked="checked" id="thumbsize_no" />
  61.     <?php _e('No thanks') ?></label>
  62.     <br />
  63.         <label for="thumbsize_small">
  64. <input type="radio" name="thumbsize" value="small" id="thumbsize_small" />
  65. <?php _e('Small (200px largest side)') ?></label>
  66.         <br />
  67.         <label for="thumbsize_large">
  68. <input type="radio" name="thumbsize" value="large" id="thumbsize_large" />
  69. <?php _e('Large (400px largest side)') ?></label>
  70.         <br />
  71.         <label for="thumbsize_custom">
  72.         <input type="radio" name="thumbsize" value="custom" id="thumbsize_custom" />
  73. <?php _e('Custom size') ?></label>
  74.       : 
  75.       <input type="text" name="imgthumbsizecustom" size="4" />
  76.     <?php _e('px (largest side)') ?>    </p>
  77.     <p><input type="submit" name="submit" value="<?php _e('Upload File') ?>" /></p>
  78.     </form>
  79. </div><?php 
  80. break;
  81. case 'upload':
  82.  
  83.     $imgalt = basename( (isset($_POST['imgalt'])) ? $_POST['imgalt'] : '' );
  84.  
  85.     $img1_name = (strlen($imgalt)) ? $imgalt : basename( $_FILES['img1']['name'] );
  86.     $img1_name = preg_replace('/[^a-z0-9_.]/i', '', $img1_name); 
  87.     $img1_size = $_POST['img1_size'] ? intval($_POST['img1_size']) : intval($_FILES['img1']['size']);
  88.  
  89.     $img1_type = (strlen($imgalt)) ? $_POST['img1_type'] : $_FILES['img1']['type'];
  90.     $imgdesc = htmlentities2($_POST['imgdesc']);
  91.  
  92.     $pi = pathinfo($img1_name);
  93.     $imgtype = strtolower($pi['extension']);
  94.  
  95.     if (in_array($imgtype, $allowed_types) == false)
  96.         die(sprintf(__('File %1$s of type %2$s is not allowed.') , $img1_name, $imgtype));
  97.  
  98.     if (strlen($imgalt)) {
  99.         $pathtofile = get_settings('fileupload_realpath')."/".$imgalt;
  100.         $img1 = $_POST['img1'];
  101.     } else {
  102.         $pathtofile = get_settings('fileupload_realpath')."/".$img1_name;
  103.         $img1 = $_FILES['img1']['tmp_name'];
  104.     }
  105.  
  106.     // makes sure not to upload duplicates, rename duplicates
  107.     $i = 1;
  108.     $pathtofile2 = $pathtofile;
  109.     $tmppathtofile = $pathtofile2;
  110.     $img2_name = $img1_name;
  111.  
  112.     while ( file_exists($pathtofile2) ) {
  113.         $pos = strpos( strtolower($tmppathtofile), '.' . trim($imgtype) );
  114.         $pathtofile_start = substr($tmppathtofile, 0, $pos);
  115.         $pathtofile2 = $pathtofile_start.'_'.zeroise($i++, 2).'.'.trim($imgtype);
  116.         $img2_name = explode('/', $pathtofile2);
  117.         $img2_name = $img2_name[count($img2_name)-1];
  118.     }
  119.  
  120.     if (file_exists($pathtofile) && !strlen($imgalt)) {
  121.         $i = explode(' ', get_settings('fileupload_allowedtypes'));
  122.         $i = implode(', ',array_slice($i, 1, count($i)-2));
  123.         $moved = move_uploaded_file($img1, $pathtofile2);
  124.         // if move_uploaded_file() fails, try copy()
  125.         if (!$moved) {
  126.             $moved = copy($img1, $pathtofile2);
  127.         }
  128.         if (!$moved) {
  129.             die(sprintf(__("Couldn't upload your file to %s."), $pathtofile2));
  130.         } else {
  131.             chmod($pathtofile2, 0666);
  132.             @unlink($img1);
  133.         }
  134.  
  135.     // 
  136.     
  137.     // duplicate-renaming function contributed by Gary Lawrence Murphy
  138.     ?>
  139.     <p><strong><?php __('Duplicate File?') ?></strong></p>
  140.     <p><b><em><?php printf(__("The filename '%s' already exists!"), $img1_name); ?></em></b></p>
  141.     <p> <?php printf(__("Filename '%1\$s' moved to '%2\$s'"), $img1, "$pathtofile2 - $img2_name") ?></p>
  142.     <p><?php _e('Confirm or rename:') ?></p>
  143.     <form action="upload.php" method="post" enctype="multipart/form-data">
  144.     <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo  get_settings('fileupload_maxk') *1024 ?>" />
  145.     <input type="hidden" name="img1_type" value="<?php echo $img1_type;?>" />
  146.     <input type="hidden" name="img1_name" value="<?php echo $img2_name;?>" />
  147.     <input type="hidden" name="img1_size" value="<?php echo $img1_size;?>" />
  148.     <input type="hidden" name="img1" value="<?php echo $pathtofile2;?>" />
  149.     <input type="hidden" name="thumbsize" value="<?php echo $_REQUEST['thumbsize'];?>" />
  150.     <input type="hidden" name="imgthumbsizecustom" value="<?php echo $_REQUEST['imgthumbsizecustom'];?>" />
  151.     <?php _e('Alternate name:') ?><br /><input type="text" name="imgalt" size="30" class="uploadform" value="<?php echo $img2_name;?>" /><br />
  152.     <br />
  153.     <?php _e('Description:') ?><br /><input type="text" name="imgdesc" size="30" class="uploadform" value="<?php echo $imgdesc;?>" />
  154.     <br />
  155.     <input type="submit" name="submit" value="<?php _e('Rename') ?>" class="search" />
  156.     </form>
  157. </div>
  158. <?php 
  159.  
  160. require('admin-footer.php');
  161. die();
  162.  
  163.     }
  164.  
  165.     if (!strlen($imgalt)) {
  166.         @$moved = move_uploaded_file($img1, $pathtofile); //Path to your images directory, chmod the dir to 777
  167.         // move_uploaded_file() can fail if open_basedir in PHP.INI doesn't
  168.         // include your tmp directory. Try copy instead?
  169.         if(!$moved) {
  170.             $moved = copy($img1, $pathtofile);
  171.         }
  172.         // Still couldn't get it. Give up.
  173.         if (!$moved) {
  174.             die(sprintf(__("Couldn't upload your file to %s."), $pathtofile));
  175.         } else {
  176.             chmod($pathtofile, 0666);
  177.             @unlink($img1);
  178.         }
  179.         
  180.     } else {
  181.         rename($img1, $pathtofile)
  182.         or die(sprintf(__("Couldn't upload your file to %s."), $pathtofile));
  183.     }
  184.     
  185.     if($_POST['thumbsize'] != 'none' ) {
  186.         if($_POST['thumbsize'] == 'small') {
  187.             $max_side = 200;
  188.         }
  189.         elseif($_POST['thumbsize'] == 'large') {
  190.             $max_side = 400;
  191.         }
  192.         elseif($_POST['thumbsize'] == 'custom') {
  193.             $max_side = intval($_POST['imgthumbsizecustom']);
  194.         }
  195.         
  196.         $result = wp_create_thumbnail($pathtofile, $max_side, NULL);
  197.         if($result != 1) {
  198.             print $result;
  199.         }
  200.     }
  201.  
  202. if ( ereg('image/',$img1_type) )
  203.     $piece_of_code = "<img src='" . get_settings('fileupload_url') ."/$img1_name' alt='$imgdesc' />";
  204. else
  205.     $piece_of_code = "<a href='". get_settings('fileupload_url') . "/$img1_name' title='$imgdesc'>$imgdesc</a>";
  206.  
  207. $piece_of_code = htmlspecialchars( $piece_of_code );
  208. ?>
  209.  
  210. <h3><?php _e('File uploaded!') ?></h3>
  211. <p><?php printf(__("Your file <code>%s</code> was uploaded successfully!"), $img1_name); ?></p>
  212. <p><?php _e('Here’s the code to display it:') ?></p>
  213. <p><code><?php echo $piece_of_code; ?></code>
  214. </p>
  215. <p><strong><?php _e('Image Details') ?></strong>: <br />
  216. Name:
  217. <?php echo $img1_name; ?>
  218. <br />
  219. <?php _e('Size:') ?>
  220. <?php echo round($img1_size / 1024, 2); ?> <?php _e('<abbr title="Kilobyte">KB</abbr>') ?><br />
  221. <?php _e('Type:') ?>
  222. <?php echo $img1_type; ?>
  223. </p>
  224. </div>
  225. <p><a href="upload.php"><?php _e('Upload another') ?></a></p>
  226. <?php
  227. break;
  228. }
  229. include('admin-footer.php');
  230. ?>